package com.leoman.plz_p.presenter;


import com.leoman.plz_p.interfaces.PointDetailView;
import com.leoman.plz_p.module.api.CourseApi;
import com.leoman.plz_p.module.bean.BaseData;
import com.leoman.plz_p.module.bean.CommentBean;
import com.leoman.plz_p.module.bean.PageListData;
import com.leoman.plz_p.module.bean.PointDetail;
import com.leoman.plz_p.subscriber.ResponseSubscriber;
import com.leoman.plz_p.utils.UserHelper;

import cn.com.corelib.api.ResponseTransformer;
import cn.com.corelib.base.BasePresenter;

public class PointDetailPresenter extends BasePresenter<PointDetailView> {

    private CourseApi courseApi;

    @Override
    protected void onViewAttach() {
        super.onViewAttach();
        courseApi=getApi(CourseApi.class);
    }


    public void getPointDetail(String courseId,String courseChapterId){
        getView().showLoading();
        courseApi.getPointDetail(UserHelper.getToken(),courseChapterId,courseId)
                .compose(new ResponseTransformer<>(this.<BaseData<PointDetail>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PointDetail>>(view) {
                    @Override
                    public void success(BaseData<PointDetail> data) {
                        view.obtainPointDetail(data.data);
                    }
                });
    }

    public void getComment(String courseChapterId,int page,int pageSize){
        courseApi.getCourseComment(courseChapterId,page,pageSize).compose(new ResponseTransformer<>(this.<BaseData<PageListData<CommentBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PageListData<CommentBean>>>(view) {
                    @Override
                    public void success(BaseData<PageListData<CommentBean>> listData) {
                        view.obtainCommentList(listData.data.getRows());
                    }

                });
    }


    public void getPointDetailByMissionClassId(String missionClassId){
        getView().showLoading();
        courseApi.getPointDetailByMissionClassId(UserHelper.getToken(),missionClassId)
                .compose(new ResponseTransformer<>(this.<BaseData<PointDetail>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PointDetail>>(view) {
                    @Override
                    public void success(BaseData<PointDetail> data) {
                        view.obtainPointDetail(data.data);
                    }
                });
    }

    public void getCommentByMissionClassId(String missionClassId,int page,int pageSize){
        courseApi.getCourseCommentByMissionClassId(UserHelper.getToken(),missionClassId,page,pageSize).compose(new ResponseTransformer<>(this.<BaseData<PageListData<CommentBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PageListData<CommentBean>>>(view) {
                    @Override
                    public void success(BaseData<PageListData<CommentBean>> listData) {
                        view.obtainCommentList(listData.data.getRows());
                    }

                });
    }

    public void sendCommentByMissionClassId(String missionClassId,String content){
        getView().showLoading();
        courseApi.sendCourseCommentByMissionClassId(UserHelper.getToken(),missionClassId,content)
                .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData>(view) {
                    @Override
                    public void success(BaseData baseData) {
                        view.obtainCommentSuccess();
                    }
                });
    }

    public void sendComment(String courseChapterId,String content){
        getView().showLoading();
        courseApi.sendCourseComment(UserHelper.getToken(),courseChapterId,UserHelper.getUserId()+"",content)
                .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData>(view) {
                    @Override
                    public void success(BaseData baseData) {
                        view.obtainCommentSuccess();
                    }
                });
    }

    @Override
    public void onStart() {

    }
}